package utilities;

/**
 * Clock 
 * It's a simple clock for the system
 * @author Camilo Andres Hernandez Marin
 *
 */

import java.util.Calendar;
import java.util.GregorianCalendar;

import windows.CInitialWindow;

public class CClock implements Runnable {
	private CInitialWindow initialWindow;
	private GregorianCalendar calendar;
	private String snow;
	private Thread thread;
	private int nday;
	private int nmonth;
	private int nyear;
	private int nhour;
	private int nminute;
	private int nsecond;
	private int namPm;

	public void iniciarReloj() {
		thread = new Thread(this);
		thread.start();
	}

	public void run() {
		String meses[] = { "Enero", "Febrero", "Marzo", "Abril", "Mayo",
				"Junio", "Julio", "Agosto", "Septiembre", "Octubre",
				"Noviembre", "Diciembre" };
		while (true) {
			snow = "";
			calendar = new GregorianCalendar();
			nday = calendar.get(Calendar.DAY_OF_MONTH);
			nmonth = calendar.get(Calendar.MONTH);
			nyear = calendar.get(Calendar.YEAR);
			nhour = calendar.get(Calendar.HOUR);
			nminute = calendar.get(Calendar.MINUTE);
			nsecond = calendar.get(Calendar.SECOND);
			namPm = calendar.get(Calendar.AM_PM);

			snow += meses[nmonth];

			if (nhour == 0)
				nhour = 12;

			snow += " " + nday + " de " + nyear + "   " + nhour + ":";

			if (nminute < 10)
				snow += "0";

			snow += nminute + ":";

			if (nsecond < 10)
				snow += "0";

			snow += nsecond + " ";

			if (namPm == 0)
				snow += "am";
			else
				snow += "pm";

			initialWindow.getReloj().setText(snow);
			try {
				thread.sleep(1000);
			} catch (Exception exce) {
			}
		}
	}

	public void setVentanaPrincipal(CInitialWindow ventanaPrincipal) {
		this.initialWindow = ventanaPrincipal;
	}

}